package floappcontest.util.converters;

import java.util.List;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;
import javax.faces.model.SelectItem;

import floappcontest.core.beans.Component;


public class ComponentConverter implements Converter {
	
	
	private final List<SelectItem> list;

	public ComponentConverter( List<SelectItem> list ) {
		this.list = list;
	}

	public Component getAsObject( @SuppressWarnings("unused")
	FacesContext context, @SuppressWarnings("unused")
	UIComponent uicomponent, String value )
			throws ConverterException {
		for ( SelectItem item : getList() ) {
			if ( item.getLabel().equalsIgnoreCase( value ) ){
				return (Component) item.getValue();
			}
		}
		return null;
	}


	public String getAsString( @SuppressWarnings("unused")
	FacesContext context, @SuppressWarnings("unused")
	UIComponent uicomponent, Object value )
			throws ConverterException {
		return ( (Component) value).getName();
	}

	
	public List<SelectItem> getList() {
		return this.list;
	}

}
